using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;


namespace DCApp
{
    public partial class ParticipantDlg : Form
    {

        const string instructionTemplate = @"Thank you for participating in the clinical research.

Participant Details
===================
Participant Name: {0}
Date Registered: {1}
Application Specific Participant Id: {2}
        
Please visit the following HealthVault shell url to login and use the participant 
identity code below to enable this clinical research to access your healthvault data.  
You will be asked the answer to the security question during that process.

Url: https://account.healthvault-ppe.com/patientwelcome.aspx
Participant Identity Code:  {3}
Security Question:  {4}
Security Answer: Please make sure you remember the answer";


        string _appSpecificParticipantId;
        public string AppSpecificParticpantId
        {
            get
            {
                return _appSpecificParticipantId;
            }
            set
            {
                _appSpecificParticipantId = value;
            }
        }

        StringReader _myReader = null;
        
        public ParticipantDlg()
        {
            InitializeComponent();
        }

        private void Form_Load(object sender, EventArgs e)
        {
            DataRow row = XmlStore.FetchParticipantFromLocalStore(_appSpecificParticipantId);
            if (row != null)
            {
                string instructions = String.Format(instructionTemplate,
                    row["ParticipantName"],
                    row["DateTokenGenerated"],
                    row["MyAppSpecificParticipantId"],
                    row["ParticipantCode"],
                    row["SecurityQuestion"]);

                txtDetails.Text = instructions;
            }
        }

        private void btnPrint_Click(object sender, EventArgs e)
        {
            printDialog1.Document = printDocument1;
            string strText = this.txtDetails.Text;
            _myReader = new StringReader(strText);
            if (printDialog1.ShowDialog() == DialogResult.OK)
            {
                this.printDocument1.Print();
            }
        }

        private void PrintDocument_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs ev)
        {
            float linesPerPage = 0;
            float yPosition = 0;
            int count = 0;
            float leftMargin = ev.MarginBounds.Left;
            float topMargin = ev.MarginBounds.Top;
            string line = null;
            Font printFont = this.txtDetails.Font;
            SolidBrush myBrush = new SolidBrush(Color.Black);

            // Work out the number of lines per page, using the MarginBounds.
            linesPerPage = ev.MarginBounds.Height / printFont.GetHeight(ev.Graphics);

            // Iterate over the string using the StringReader, printing each line.
            while (count < linesPerPage && ((line = _myReader.ReadLine()) != null))
            {
                // calculate the next line position based on 
                // the height of the font according to the printing device
                yPosition = topMargin + (count * printFont.GetHeight(ev.Graphics));

                // draw the next line in the rich edit control
                ev.Graphics.DrawString(line, printFont, myBrush, leftMargin, yPosition, new StringFormat());
                count++;
            }

            //If there are more lines, We need print another page. 
            //Since _myReader is global to this class, we just need to set the
            //PrintPageEventArgs's HasMorePages to true so that this function will be called again
            //and the remaining lines in _myReader is sent to printer

            if (line != null)
                ev.HasMorePages = true;
            else
                ev.HasMorePages = false;

            myBrush.Dispose();
        }
    }
}